//教练使用整数数组 actions 记录一系列核心肌群训练项目编号。为增强训练趣味性，需要将所有奇数编号训练项目调整至偶数编号训练项目之前。请将调整后的训练项
//目编号以 数组 形式返回。 
//
// 
//
// 示例 1： 
//
// 
//输入：actions = [1,2,3,4,5]
//输出：[1,3,5,2,4] 
//解释：为正确答案之一 
//
// 
//
// 提示： 
//
// 
// 0 <= actions.length <= 50000 
// 0 <= actions[i] <= 10000 
// 
//
// 
//
// Related Topics 数组 双指针 排序 👍 339 👎 0


package LeetCode.editor.cn;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author ldltd
 * @date 2025-04-29 15:30:44
 * @description LCR 139.训练计划 I
 
 */
 
public class DiaoZhengShuZuShunXuShiQiShuWeiYuOuShuQianMianLcof {
    public static void main(String[] args) {
    //测试代码
    DiaoZhengShuZuShunXuShiQiShuWeiYuOuShuQianMianLcof fun = new DiaoZhengShuZuShunXuShiQiShuWeiYuOuShuQianMianLcof();
    Solution solution= fun.new Solution();
    
    }

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
        //原地修改，但是会破坏原始顺序
    public int[] trainingPlan1(int[] actions) {
        if (actions == null || actions.length == 0) {
            return actions;
        }

        int left = 0;                   // 左指针（找偶数）
        int right = actions.length - 1; // 右指针（找奇数）

        while (left < right) {
            // 左指针找到偶数
            while (left < right && actions[left] % 2 != 0) {
                left++;
            }
            // 右指针找到奇数
            while (left < right && actions[right] % 2 == 0) {
                right--;
            }
            // 交换
            if (left < right) {
                int temp = actions[left];
                actions[left] = actions[right];
                actions[right] = temp;
                left++;
                right--;
            }
        }

        return actions;
    }
    //原始顺序，但是需要额外空间
    public int[] trainingPlan2(int[] actions) {
        if (actions == null || actions.length == 0) {
            return actions;
        }

        List<Integer> odds = new ArrayList<>();  // 奇数
        List<Integer> evens = new ArrayList<>(); // 偶数

        for (int num : actions) {
            if (num % 2 != 0) {
                odds.add(num);
            } else {
                evens.add(num);
            }
        }

        // 合并
        int[] result = new int[actions.length];
        int index = 0;
        for (int odd : odds) {
            result[index++] = odd;
        }
        for (int even : evens) {
            result[index++] = even;
        }

        return result;
    }

    public int[] trainingPlan(int[] actions) {
        return Arrays.stream(actions)
                .boxed() // int -> Integer
                .sorted(Comparator.comparingInt(a -> a % 2 == 0 ? 1 : -1)) // 奇数在前，偶数在后
                .mapToInt(Integer::intValue)
                .toArray();
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
